//
//  CZRetweetView.swift
//  项目框架
//
//  Created by yang on 16/3/2.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZRetweetView: UIView {

    //记录一个约束
    var bottomConstaint: Constraint?
    //定义一个属性
    var retweetViewModel: CZStatusViewModel?{
        
        didSet{
            
            contentLabel.text = retweetViewModel?.retweetStatusString
            
            //数据这里来判断,如果有转发的配图则显示转发配图,
            //没有转发配图,则不显示配图
            
            //转发微博的图片都在转发微博字段里
            
            //清空原有的约束
            self.bottomConstaint?.uninstall()
            
            if let pic_url = retweetViewModel?.statusModel?.retweeted_status?.pic_urls where pic_url.count > 0{
            
                pictureView.hidden = false
                
                //更新配图的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
            //定义一个数组接收数据
                var result = [CZPictureViewModel]()
                //遍历
                for model in pic_url{
                
                let viewModel = CZPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                
                }
            //赋值
                pictureView.pictureList = result

            }else{
            
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
            
            
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    
    private func setupUI(){
        
        backgroundColor = UIColor.yellowColor()
        
        addSubview(contentLabel)
        addSubview(pictureView)

        
        // 设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            //make.bottom.equalTo(self.snp_bottom)
            
        }
        //默认都有转发图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(CellMargin)
            
            make.top.equalTo(contentLabel.snp_bottom)
            
            //给他一个固定的宽高
            make.size.equalTo(CGSizeMake(100, 100))
        }
        //转发微博底部的视图
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }
    
    //MARK:--懒加载
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "转发微博"
        
        label.textColor = UIColor.redColor()
        
        label.numberOfLines = 0
        
        label.font = UIFont.systemFontOfSize(14)
        
        return label
        
    }()
    //配图
    private lazy var pictureView: CZPictureView = CZPictureView()
}
